package com.kx.kxpicture.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kx.kxpicture.annotation.AuthCheck;
import com.kx.kxpicture.common.BaseResponse;
import com.kx.kxpicture.common.DeleteRequest;
import com.kx.kxpicture.common.ResultUtils;
import com.kx.kxpicture.constant.UserLoginConstant;
import com.kx.kxpicture.exception.BusinessException;
import com.kx.kxpicture.exception.ErrorCode;
import com.kx.kxpicture.exception.ThrowsUtils;
import com.kx.kxpicture.model.dto.user.*;
import com.kx.kxpicture.model.entity.User;
import com.kx.kxpicture.model.vo.UserLoginVO;
import com.kx.kxpicture.model.vo.UserVO;
import com.kx.kxpicture.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户控制器
 *
 * @author kuangxin
 * date 2025/8/15
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterDTO userRegisterDTO){
        ThrowsUtils.throwIf(userRegisterDTO == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        Long userId = userService.userRegister(userRegisterDTO);
        return ResultUtils.success(userId);
    }

    @PostMapping("/login")
    public BaseResponse<UserLoginVO> userLogin(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request){
        ThrowsUtils.throwIf(userLoginDTO == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        UserLoginVO userLoginVO = userService.userLogin(userLoginDTO.getUserAccount(), userLoginDTO.getUserPassword(),request);
        return ResultUtils.success(userLoginVO);
    }

    @GetMapping("/get/login")
    public BaseResponse<UserLoginVO> getLoginUser(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(loginUser, userLoginVO);
        return ResultUtils.success(userLoginVO);
    }

    @GetMapping("/logout")
    public BaseResponse<Boolean> userLogOut(HttpServletRequest request){
        boolean loginUser = userService.userLogout(request);
        return ResultUtils.success(loginUser);
    }

    @AuthCheck(mustRole = UserLoginConstant.USER_ADMIN)
    @PostMapping("/add")
    public BaseResponse<Long> userLogin(@RequestBody UserAddRequestDTO userAddRequestDTO){
        ThrowsUtils.throwIf(userAddRequestDTO == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        User user= new User();
        BeanUtils.copyProperties(userAddRequestDTO,user);
        user.setUserPassword(userService.getEncryptPassword("12345678"));
        boolean save = userService.save(user);
        ThrowsUtils.throwIf(!save,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(user.getId());
    }

    /**
     * 根据id获取用户
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserLoginConstant.USER_ADMIN)
    public BaseResponse<User> getUserById(Long id){
        ThrowsUtils.throwIf(id<=0,ErrorCode.PARAMS_ERROR);
        User user = userService.getById(id);
        ThrowsUtils.throwIf(ObjUtil.isNull(user),ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }

    /**
     * 根据id获取包装类供用户
     */
    @GetMapping("/get/vo")
    public BaseResponse<UserVO> getUserVOById(Long id){
        BaseResponse<User> userBaseResponse = getUserById(id);
        User user = userBaseResponse.getData();
        return ResultUtils.success(userService.getUserToVo(user));
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole=UserLoginConstant.USER_ADMIN)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = userService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }


    @AuthCheck(mustRole = UserLoginConstant.USER_ADMIN)
    @PostMapping("/update")
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequestDTO userUpdateRequestDTO){
        ThrowsUtils.throwIf(userUpdateRequestDTO == null, ErrorCode.PARAMS_ERROR);
        User user= new User();
        BeanUtils.copyProperties(userUpdateRequestDTO,user);
        boolean save = userService.updateById(user);
        ThrowsUtils.throwIf(!save,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(save);
    }

    /**
     * 分页获取用户数据
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserLoginConstant.USER_ADMIN)
    public BaseResponse<Page<UserVO>> getUserVOList(@RequestBody UserQueryRequestDTO userQueryRequestDTO){
        QueryWrapper<User> queryWrapper = userService.getQueryWrapper(userQueryRequestDTO);
        long current = (long) userQueryRequestDTO.getCurrent();
        long pageSize = userQueryRequestDTO.getPageSize();
        Page<User> userPage=new Page<>(current,pageSize);
        Page<User> page = userService.page(userPage, queryWrapper);
        long total = page.getTotal();
        List<User> records = page.getRecords();
        List<UserVO> userVOList = userService.getUserVOList(records);
        Page<UserVO> userVOPage=new Page<>(current,pageSize,total);
        userVOPage.setRecords(userVOList);
        return ResultUtils.success(userVOPage);
    }
}
